package com.ztesoft.book.core.sys.sysloginhis.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ztesoft.book.core.base.base.service.impl.BaseExServiceImpl;
import com.ztesoft.book.core.sys.sysdict.dto.SysDictDTO;
import com.ztesoft.book.core.sys.sysdict.enums.SysDictStateEnum;
import com.ztesoft.book.core.sys.sysdict.service.SysDictService;
import com.ztesoft.book.core.sys.sysloginhis.SysLoginHisVO;
import com.ztesoft.book.core.sys.sysloginhis.dto.SysLoginHisDTO;
import com.ztesoft.book.core.sys.sysloginhis.mapper.SysLoginHisMapper;
import com.ztesoft.book.core.sys.sysloginhis.qo.SysLoginHisQO;
import com.ztesoft.book.core.sys.sysloginhis.service.SysLoginHisService;
import com.ztesoft.book.core.sys.sysuser.dto.SysUserDTO;
import com.ztesoft.book.core.sys.sysuser.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 登录记录表 登录记录表管理
 *
 * @author 童国光
 * 创建时间：2021-07-26
 */
@Service("SysLoginHisService")
public class SysLoginHisServiceImpl extends BaseExServiceImpl<SysLoginHisMapper, SysLoginHisDTO, SysLoginHisQO> implements SysLoginHisService {

    @Resource
    private SysLoginHisMapper sysLoginHisMapper;

    @Resource
    private SysDictService sysDictService;

    @Resource
    private SysUserMapper sysUserMapper;

    private static final String UPDATE_USER_DELETE = "update_user_delete";

    /**
     * getMapper
     *
     * @return SysLoginHisMapper
     */
    @Override
    protected SysLoginHisMapper getMapper() {
        return sysLoginHisMapper;
    }

    /**
     * 用户三个月未登录，自动转换到停用转态
     */
    @Override
    public void updateUserDeleted() {
        List<SysDictDTO> sysDictList = sysDictService.getSysDictListByTypeCode(UPDATE_USER_DELETE);
        if (CollUtil.isNotEmpty(sysDictList) && ObjectUtil.isNotEmpty(sysDictList.get(0))) {
            SysDictDTO dict = sysDictList.get(0);
            // 判断是否启用
            if (SysDictStateEnum.NORMAL.getValue()
                    .equals(dict.getState()) && StrUtil.isNotEmpty(dict.getRemark())) {
                // 查询所有人的id及其最近一次登录时间信息
                List<SysUserDTO> sysUserList = sysUserMapper.queryUserIdAndLoginTime();
                if (CollUtil.isNotEmpty(sysUserList)) {
                    LocalDateTime now = LocalDateTime.now();
                    List<String> ids = new ArrayList<>();
                    for (SysUserDTO user : sysUserList) {
                        if (ObjectUtil.isNotEmpty(user.getLoginTime())) {
                            LocalDateTime loginTime = user.getLoginTime();
                            Long days = Duration.between(loginTime, now)
                                    .toDays();
                            if (days >= Long.parseLong(dict.getRemark())) {
                                ids.add(user.getId());
                            }
                        }
                    }
                    if (CollUtil.isNotEmpty(ids)) {
                        // 批量跟新用户状态为停用
                            sysUserMapper.batchUpdateUserDeleteByIds(ids);
                    }
                }
            }
        }
    }

    /**
     * 保存登录记录
     *
     * @param his his
     */
    @Override
    public void saveSysLoginHis(SysLoginHisVO his) {
        getMapper().saveSysLoginHis(his);
    }

}
